package com.ysw.drivers.controller;

import com.ysw.drivers.pojo.User;
import com.ysw.drivers.service.DBChangeService;
import com.ysw.drivers.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ysw
 */
@RestController
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private DBChangeService dbChangeService;

    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public String test() throws Exception {
        //切换到数据源dbtest2
        String datasourceId = "dbtest2";
        dbChangeService.changeDb(datasourceId);
        List<User> users = userService.queryUserInfo();
        LOG.info("数据源dbtest2查询到的数据为: {}", users);

        //再切换到数据dbtest3
        String datasourceId2 = "dbtest3";
        dbChangeService.changeDb(datasourceId2);
        List<User> users2 = userService.queryUserInfo();
        LOG.info("数据dbtest3查询到的数据为: {}", users2);

        return "OK";
    }

}
